#include "CSetAnimationFileCommand.h"
#include "CSetMeshFileCommand.h"
#include "CUserInterface.h"
#include "CLevelEditor.h"
#include "IGUIEnvironment.h"
//#include "IColladaSceneNodeAnimator.h"
//#include "CColladaCameraSceneNode.h"
#include "FpsColladaDatabase.h"
//#include "CColladaSceneNodeAnimator.h"
#include "CSceneObject.h"
#include "IAttributes.h"

extern CGlobals* g;
extern CUserInterface* ui;
extern CLevelEditor* editor;
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
CSetAnimationFileCommand::CSetAnimationFileCommand (int id, stringc newAnimationFile ,bool create )
{
	m_id = id;
	CSceneObject* obj = editor->getSceneObjectFromID(id);	
	m_oldAnimationFile = obj->m_UserData->getAttributeAsString("AnimationFile");
	m_newAnimationFile = newAnimationFile;
	setDescription(L"change AnimationFile");
	bCreateProps = create;
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetAnimationFileCommand::redo ()
{
	doSet(m_newAnimationFile);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetAnimationFileCommand::undo ()
{
	doSet(m_oldAnimationFile);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void CSetAnimationFileCommand::doSet(stringc animFile)
{
	CSceneObject* obj = editor->getSceneObjectFromID(m_id);	
	
	irr::collada::CSceneNodeAnimator* _animatorNode = NULL;

	if (obj->m_UserData->findAttribute("AnimationFile") >= 0)
	{		
		obj->m_UserData->setAttribute("AnimationFile", animFile.c_str());
		
		if (animFile != "")
		{
			_animatorNode = NULL;//ConstructColladaAnimator(animFile.c_str());
			if (!_animatorNode)
			{
				stringw strError = L"ObjectID ";
				strError += m_id;
				strError += L" cannot load AnimationFile : '";
				strError += stringw(animFile.c_str());
				strError += "'!!!";
				//g->guienv->addMessageBox(L"Error!", strError.c_str(), false);
				DEBUG_OUT( "Error %s " , stringc(strError.c_str()).c_str() );
			}
			else
			{
				//_animatorNode->grab();
			}
		}
		//load animator from mesh
		else
		{
			stringc str = obj->m_UserData->getAttributeAsString("MeshFile");
			CSetMeshFileCommand* cmd = new CSetMeshFileCommand(m_id, str);
			cmd->doSet(str, false);
			cmd->drop();
		}
	}

//	obj->SetMeshAndAnimator(obj->m_SceneNode, _animatorNode);

	//if (obj->HasAnim())
	//{
	//	obj->SetAnim(0 , false );
	//}
	
	if (bCreateProps)
	{
		editor->setSelectedNode(obj->m_SceneNode);
		ui->createSelectedObjectProperties();
	}

}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
